package light.mvc.controller.sys;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import light.mvc.controller.base.BaseController;
import light.mvc.framework.constant.GlobalConstant;
import light.mvc.model.workflow.Tfield;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.SessionInfo;
import light.mvc.pageModel.sys.User;
import light.mvc.service.sys.UserServiceI;
import light.mvc.utils.LightmvcConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

@Controller
@RequestMapping("/admin")
public class IndexController extends BaseController {

	@Autowired
	private UserServiceI userService;

	@RequestMapping("/index")
	public String index(HttpServletRequest request) {
		SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		if ((sessionInfo != null) && (sessionInfo.getId() != null)) {
			return "/index";
		}
		return "/login";
	}
	
	@RequestMapping("/setPeopleStatusUI")
	public String setPeopleStatusUI(HttpServletRequest request) {
		return "/setPeopleStatusUI";
	}
	
	@RequestMapping("/showPeopleStatusDetailUI")
	public String showPeopleStatusDetailUI(HttpServletRequest request) {
		return "/showPeopleStatusDetailUI";
	}
	
	@RequestMapping("/setPeopleStatusUIData")
	@ResponseBody
	public Json setPeopleStatusUIData(User user) {
		Json json = new Json();
		try {
			userService.myUpdate(user);
			json.setSuccess(true);
			json.setMsg("更新成功");
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg("更新失败");
		}
		return json;
	}

	@RequestMapping("/login")
	@ResponseBody
	public Json login(User user, HttpSession session) {
		Json j = new Json();
		User sysuser = userService.login(user);
		if (sysuser != null) {
			j.setSuccess(true);
			j.setMsg("登陆成功！");

			SessionInfo sessionInfo = new SessionInfo();
			sessionInfo.setId(sysuser.getId());
			sessionInfo.setLoginName(sysuser.getLoginName());
			sessionInfo.setName(sysuser.getName());
			// user.setIp(IpUtil.getIpAddr(getRequest()));
			WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();  
	        ServletContext servletContext = webApplicationContext.getServletContext();
			sessionInfo.setResourceAllList((List<String>) servletContext.getAttribute("resourceList"));
			sessionInfo.setResourceList(userService.resourceList(sysuser.getId()));
			session.setAttribute(GlobalConstant.SESSION_INFO, sessionInfo);
		} else {
			j.setMsg("用户名或密码错误！");
		}
		return j;
	}

	@ResponseBody
	@RequestMapping("/logout")
	public Json logout(HttpSession session) {
		Json j = new Json();
		if (session != null) {
			session.invalidate();
		}
		j.setSuccess(true);
		j.setMsg("注销成功！");
		return j;
	}
	
	@ResponseBody
	@RequestMapping("/getFields")
	public List<Tfield> getFields(HttpSession session) {
		List<Tfield> names = new ArrayList<Tfield>();
		Field[] fields = LightmvcConstants.class.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			try {
				Tfield tfield = new Tfield();
				tfield.setId(Long.valueOf(i + ""));
				tfield.setName((String) fields[i].get(new LightmvcConstants()));
				names.add(tfield);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return names;
	}
}
